package com.imu.wys.dao;

import com.imu.wys.model.User;
import com.imu.wys.util.DBUtil;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UserLogDao {

    public void add(User user,String useraction)  {
        Connection connection= DBUtil.getConnection();
        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;


        //获取系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String action_time= df.format(new Date());
        String userIP ="0.0.0.0";
        //获取ip
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            userIP = ip4.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String sql ="insert into j1_userlog(userid,username,userip,useraction,action_time) values (?,?,?,?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, user.getUserid());
            preparedStatement.setString(2, user.getUsername());
            preparedStatement.setString(3, userIP);
            preparedStatement.setString(4, useraction);
            preparedStatement.setString(5, action_time);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }



    }

}
